﻿using System;
using System.Collections.Generic;
using System.Text;

namespace Catalina.Common
{
	public class CatalinaLabel : IComparable<CatalinaLabel>
	{
		// ----------------------------------------------------------
		// Constructor
		#region ...
		public CatalinaLabel(
			String upc,
			String description,
			String requirements,
			String incentive,
			DateTime endDate)
		{
			this.productDescription = description;
			this.requirements = requirements;
			this.incentive = incentive;
			this.upc = upc;
			this.endDate = endDate;
		}

		public CatalinaLabel(
			String upc, 
			String description, 
			String requirements, 
			String incentive, 
			int locAisle,
			int locBlock,
			int locSeq,
			DateTime endDate)
		{
			this.productDescription = description;
			this.requirements = requirements;
			this.incentive = incentive;
			this.upc = upc;
			this.endDate = endDate;
			this.locSeq = locSeq;
			this.locAisle = locAisle;
			this.locBlock = locBlock;
		}

		#endregion

		// ----------------------------------------------------------
		// Members
		#region ...

		private String productDescription;
		private String requirements;
		private String incentive;
		private String upc;
		private DateTime endDate;
		private int locAisle;
		private int locBlock;
		private int locSeq;

		#endregion
		
		// ----------------------------------------------------------
		// Properties
		#region ...

		public String Description
		{
			get { return this.productDescription; }
		}

		public String Requirements
		{
			get { return this.requirements; }
		}

		public String Incentive
		{
			get { return this.incentive; }
		}

		public String Upc
		{
			get { return this.upc; }
		}

		public String TagSubscription
		{
			get { return locAisle.ToString() + "-" + locBlock.ToString() + "-" + locSeq.ToString(); }
		}

		public DateTime EndDate
		{
			get { return this.endDate; }
		}

		#endregion

		public override string ToString()
		{
			return "\"" + ReadableUpc(upc) + "\",\"" + productDescription + "\",\"" + requirements + "\",\"" + incentive + "\",\"" + endDate.ToString("MM/dd/yy") + "\",\"" + TagSubscription + "\"";
		}

		private String ReadableUpc(string upc)
		{
			String result = upc;
			while (result[0] == '0')
				result = result.Substring(1);
			
			return result.Insert(5, "-");
		}

		public int CompareTo(CatalinaLabel other)
		{
			if (this.locAisle == 0 && other.locAisle == 0)
			{
				return this.upc.CompareTo(other.upc);
			}

			if (this.locAisle == 0 && other.locAisle != 0)
				return 1;

			if (this.locAisle != 0 && other.locAisle == 0)
				return -1;

			if (this.locAisle < other.locAisle)
				return -1;

			if (this.locAisle > other.locAisle)
				return 1;

			if (this.locBlock < other.locBlock)
				return -1;

			if (this.locBlock > other.locBlock)
				return 1;

			if (this.locSeq < other.locSeq)
				return -1;

			if (this.locSeq > other.locSeq)
				return 1;

			return this.upc.CompareTo(other.upc);
		}
	}
}
